package design_patterns.command;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;

import java.util.logging.Logger;

import static design_patterns.command.CommandState.FINISHED;

/**
 * Created by nuno-rafael-figueiredo on 01-07-2014.
 */
public abstract class Command extends SimpleObjectProperty<CommandState> implements ICommand{
    protected transient final Logger logger = Logger.getLogger(getClass().getName());

    protected void finish() {
        setValue(FINISHED);
    }

    public Command onFinish(ChangeListener<CommandState> listener){
        addListener((observable, oldValue, newValue) -> {
            if (newValue == FINISHED)
                listener.changed(observable, oldValue, newValue);
        });

        return this;
    }

    protected Command bind(Command command) {
        command.onFinish((observable, oldValue, newValue) -> finish());
        return command;
    }
}
